﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using SmartSpace.Core;
using SmartSapce.ServiceLocation;
using SmartSpace.Packaging;

namespace SmartSpace.WebApp._console.core
{
    public partial class org_upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region 上传
        protected void _ctlBtnImport_Click(object sender, EventArgs e)
        {
            Stream stream = new MemoryStream(_ctlFileXml.FileBytes);
            List<Org> orgs = XOrgUtil.Load(stream);
            ICoreService service = ServiceLocator.Get<ICoreService>();

            Stack<Org> stack = new Stack<Org>();
            foreach (var item in orgs)
            {
                stack.Push(item);
            }

            while (stack.Count > 0)
            {
                Org org = stack.Pop();
                service.SaveOrg(org);

                foreach (var subOrg in org.Children)
                {
                    stack.Push(subOrg);
                }
            }

            Response.Redirect(Request.Url.OriginalString);
        }
        #endregion
    }
}